import torch
import torch.nn as nn
import torch.nn.functional as F


class LeNet5(nn.Module):
    def __init__(self, num_classes: int):
        super().__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, num_classes)

    def forward(self, x: torch.Tensor):
        # [3,32,32]->conv1->[6,28,28]->pool->[6,14,14]
        x = self.pool(F.relu(self.conv1(x)))
        # [6,14,14]->conv2->[16,10,10]->pool->[16,5,5]
        x = self.pool(F.relu(self.conv2(x)))
        # [16,5,5]->[400]
        x = torch.flatten(x, 1)
        # [400]->fc1->[120] 
        x = F.relu(self.fc1(x))
        # [120]->fc2->[84] 
        x = F.relu(self.fc2(x))
        # [84]->fc3->[num_classes] 
        x = self.fc3(x)
        return x
